//
//  BaseLog.m
//  LotteryClient
//
//  Created by work-li on 14-3-19.
//  Copyright (c) 2014年 CTTC. All rights reserved.
//

#import "BaseLog.h"
#import <UIKit/UIKit.h>

#define kLogTxtName @"appLog.txt"

@interface BaseLog ()
@end

@implementation BaseLog

+ (void)systemLogOutput:(const char *)name
           functionName:(const char *)functionName
                lineNum:(int)lineNum
          outPutContent:(NSString *)content
{
    NSString* file = [[NSString alloc] initWithFormat:@"%s", name];
    NSString *fileType = nil;
    NSString *fileContent = [NSString stringWithFormat:@"%s [Line %d] %@", functionName, lineNum, content];
    if(kAppLogLevel == 5) //只写文件
    {
        [[self class] writeFile:fileContent];
    }
    else if(kAppLogLevel == 6)
    {
        NSLog(@"%@",fileContent);
    }
    else //只输出控制台
    {
        BOOL isLogTxt = YES;
        NSString *fileName = [file lastPathComponent];
        if(kAppLogLevel == 0)
        {
            fileType = @"Controller";
        }
        else if(kAppLogLevel == 1)
        {
            fileType = @"Business";
        }
        else if(kAppLogLevel == 2)
        {
            fileType = @"Service";
        }
        else if(kAppLogLevel == 3) //除了上述的几种类型写文件
        {
            fileType = @"";
        }
        else if(kAppLogLevel == 4)
        {
            isLogTxt = true;
        }
        if(fileType.length > 0)
        {
            NSRange theRange = [fileName rangeOfString:fileType];
            if(theRange.length <= 0)
            {
                fileContent = nil;
            }
        }
        else if([fileType isEqualToString:@""])
        {
            if([fileName rangeOfString:@"Controller"].length > 0 || [fileName rangeOfString:@"Business"].length > 0 || [fileName rangeOfString:@"Service"].length > 0){
                fileContent = nil;
            }
        }
        
        if (fileContent.length > 0)
        {
            NSLog(@"%@",fileContent);
            if (isLogTxt == true)
            {
                [[self class] writeFile:fileContent];
            }
        }
    }

}

+ (void)writeFile:(NSString *)fileContent
{
    if(fileContent != nil)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString * path=[documentsDirectory stringByAppendingPathComponent:kLogTxtName];
        fileContent = [NSString stringWithFormat:@"%@\n",fileContent];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:path] == NO)
        {
            [fileContent writeToFile:path
                          atomically:YES
                            encoding:NSUTF8StringEncoding
                               error:nil];
        }
        else
        {
            NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path];
            [fileHandle seekToEndOfFile];
            NSData* stringData  = [fileContent dataUsingEncoding:NSUTF8StringEncoding];
            [fileHandle writeData:stringData];
            [fileHandle closeFile];
        }
    }
}
@end
